package mapper

import (
	"com.yy.api/server/models"
	"gorm.io/gorm"
)

type GenericMapper[T any] interface {
	Create(model *T) error
	Update(model *T) error
	Delete(model *T) error
}

type GenericMapperImpl[T any] struct {
	DB *gorm.DB
}

func newGenericMapper[T any](db *gorm.DB) *GenericMapperImpl[T] {
	return &GenericMapperImpl[T]{
		DB: db,
	}
}
func (m *GenericMapperImpl[T]) Create(model *T) error {
	return m.DB.Create(model).Error
}
func (m *GenericMapperImpl[T]) Update(model *T) error {
	return m.DB.Save(model).Error
}
func (m *GenericMapperImpl[T]) Delete(model *T) error {
	return m.DB.Delete(model).Error
}

func NewContainerMapper(db *gorm.DB) ContainerMapper {
	return &ContainerMapperImpl{
		GenericMapperImpl: newGenericMapper[models.ContainerBasic](db),
	}
}

func NewImageMapper(db *gorm.DB) ImageMapper {
	return &ImageMapperImpl{
		GenericMapperImpl: newGenericMapper[models.ImageBasic](db),
	}
}
func NewUserMapper(db *gorm.DB) UserMapper {
	return &UserMapperImpl{
		GenericMapperImpl: newGenericMapper[models.UserBasic](db),
	}
}

//func init() {
//	db := config.GetDBConnection()
//	db.AutoMigrate(&models.ContainerBasic{}, &models.ImageBasic{}, &models.UserBasic{})
//}
